package com.qc.wintrax.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ViewTreeObserver;
import android.widget.RelativeLayout;

/**
 * Created by Administrator on 2016/9/8.
 */

public class FractionTranslateLayout extends RelativeLayout {
    //    private float yFraction = 0;
//    private float xFraction = 0;
//
    private int screenWidth;
    private float fractionX;
    private OnLayoutTranslateListener onLayoutTranslateListener;

    public FractionTranslateLayout(Context context) {
        super(context);
    }

    public FractionTranslateLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FractionTranslateLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    protected void onSizeChanged(int w, int h, int oldW, int oldH){

        // Assign the actual screen width to our class variable.
        screenWidth = w;

        super.onSizeChanged(w, h, oldW, oldH);
    }

    public float getFractionX(){
        return fractionX;
    }

    public void setFractionX(float xFraction){
        this.fractionX = xFraction;

        // When we modify the xFraction, we want to adjust the x translation
        // accordingly.  Here, the scale is that if xFraction is -1, then
        // the layout is off screen to the left, if xFraction is 0, then the
        // layout is exactly on the screen, and if xFraction is 1, then the
        // layout is completely offscreen to the right.
        if (screenWidth > 0) setX((xFraction * screenWidth));
        else setX(0);

        if (xFraction == 1 || xFraction == -1) {
            setAlpha(0);
        } else if (xFraction < 1 /* enter */|| xFraction > -1 /* exit */) {
            if (getAlpha() != 1) {
                setAlpha(1);
            }
        }

        if (onLayoutTranslateListener != null) {
            onLayoutTranslateListener.onLayoutTranslate(this, xFraction);
        }
    }

    public void setOnLayoutTranslateListener(OnLayoutTranslateListener onLayoutTranslateListener) {
        this.onLayoutTranslateListener = onLayoutTranslateListener;
    }

    public static interface OnLayoutTranslateListener {
        void onLayoutTranslate(FractionTranslateLayout view, float xFraction);
    }
//
//    private ViewTreeObserver.OnPreDrawListener preDrawListener = null;
//
//    public void setYFraction(float fraction)
//    {
//        this.yFraction = fraction;
//        if (getHeight() == 0)
//        {
//            if (preDrawListener == null)
//            {
//                preDrawListener = new ViewTreeObserver.OnPreDrawListener()
//                {
//                    @Override
//                    public boolean onPreDraw()
//                    {
//                        getViewTreeObserver().removeOnPreDrawListener(preDrawListener);
//                        setYFraction(yFraction);
//                        return true;
//                    }
//                };
//                getViewTreeObserver().addOnPreDrawListener(preDrawListener);
//            }
//            return;
//        }
//        float translationY = getHeight() * fraction;
//        Log.v("translationY set", translationY + " ");
//        setTranslationY(translationY);
//    }
//
//    /** * 支持XFraction属性动画，以下都类似 */
//    public void setXFraction(float fraction)
//    {
//        this.xFraction = fraction;
//        if (getWidth() == 0)
//        {
//            if (preDrawListener == null)
//            {
//                preDrawListener = new ViewTreeObserver.OnPreDrawListener()
//                {
//                    @Override
//                    public boolean onPreDraw()
//                    {
//                        getViewTreeObserver().removeOnPreDrawListener(preDrawListener);
//                        setXFraction(xFraction);
//                        return true;
//                    }
//                };
//                getViewTreeObserver().addOnPreDrawListener(preDrawListener);
//            }
//            return;
//        }
////        setGlide(fraction);
//        float translationX = getWidth() * fraction;
//        setTranslationX(translationX);
//    }
//
//    public void setGlide(float fraction)
//    {
//        float translationX = getWidth() * fraction;
//        setTranslationX(translationX);
//        setRotationY(90 * fraction);
//        setPivotX(0);
//    }
//
//    public void setGlideBack(float fraction)
//    {
//        float translationX = getWidth() * fraction;
//        setTranslationX(translationX);
//        setRotationY(90 * fraction);
//        setPivotX(0);
//        setPivotY(getHeight() / 2);
//    }
}